// pages/searchResult/searchResult.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [], // 商品列表
    noMore: false, // 没有更多了？
    isNone: false, // 空列表？
    page: 1,
    pagesize: 10,
    sales_order: '', // 销量排序
    price_order: '', // 价格排序 1降序 2升序
    comprehensive_order: 1, // 综合排序
    keywords: '', // 搜索关键词
    schoolId: '', // 校区
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      keywords: options.key
    })

    if (options.schoolId){
      this.setData({
        schoolId: options.schoolId
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initData();
    this.getData();
  },

  // 获取数据
  getData(){
    app.loading();
    let that = this;
    wx.request({
      url: app.baseURL + 'item/getItemLists',
      data: {
        page: that.data.page,
        pagesize: that.data.pagesize,
        sales_order: that.data.sales_order,
        price_order: that.data.price_order,
        comprehensive_order: that.data.comprehensive_order,
        keyword: that.data.keywords,
        school_id: this.data.schoolId,
        token: wx.getStorageSync('userId') ? wx.getStorageSync('userId') : '',
      },
      success(data){
        let res = data.data;
        wx.hideLoading();
        if(res.code == 1){
          if(res.data.list.length > 0){
            that.setData({
              list: [...that.data.list,...res.data.list],
              noMore: Number(res.data.list.length) >= Number(that.data.pagesize) ? false : true
            })
          }

          if(that.data.list.length > 0){
            that.setData({
              isNone: false
            })
          }else{
            that.setData({
              isNone: true
            })
          }

        }else{
          app.$tip(res.msg)
        }
      }
    })
  },

  // 格式化
  initData(){
    this.setData({
      page: 1,
      list: [],
    })
  },

  // 返回搜索
  toSearch(){
    wx.navigateBack();
  },

  // 综合排序
  searchAll() {
    this.setData({
      comprehensive_order: 1,
      sales_order: '',
      price_order: ''
    })
    this.initData();
    this.getData();
  },

  // 销量排序
  searchSales() {
    this.setData({
      comprehensive_order: '',
      sales_order: 1,
      price_order: ''
    })
    this.initData();
    this.getData();
  },

  // 价格排序
  searchPrices() {
    // 默认降序
    if (!this.data.price_order) {
      this.setData({
        comprehensive_order: '',
        sales_order: '',
        price_order: 1
      })
      this.initData();
      this.getData();
      return
    }
    // 切换升序
    if (this.data.price_order == 1) {
      this.setData({
        comprehensive_order: '',
        sales_order: '',
        price_order: 2
      })
      this.initData();
      this.getData();
      return
    }
    // 切换降序
    if (this.data.price_order == 2) {
      this.setData({
        comprehensive_order: '',
        sales_order: '',
        price_order: 1
      })
      this.initData();
      this.getData();
      return
    }
  },

  // 去商品详情
  toDetail(e){
    let item = e.currentTarget.dataset.item;
    if (this.data.schoolId){
        wx.navigateTo({
          url: '/pages/school_detail/school_detail?id=' + item.goods_id + '&school=' + this.data.schoolId,
        })
    }else{
      // 普通商品
      if (item.is_marketing == 0){
        wx.navigateTo({
          url: '/pages/goods_detail/goods_detail?id=' + item.goods_id,
        })
      }else{
        // 活动商品
        wx.navigateTo({
          url: '/pages/kill_detail/kill_detail?id=' + item.goods_id + '&actId=' + item.marketing_id,
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.noMore){
      this.setData({
        page: Number(this.data.page) + 1
      })
      this.getData();
    }
  },
})